package com.swsm.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.swsm.Constant;
import com.swsm.model.UserModel;
import com.swsm.model.bean.B2SUserBean;
import com.swsm.service.UserService;
import com.swsm.utils.CUtils;
import com.swsm.utils.PinYinUtils;
import com.swsm.utils.ThreadPool;
import com.swsm.utils.UserParse;

@Controller
public class UserController {
	
	private Logger logger = LoggerFactory.getLogger(UserController.class);
	private static final String PROGRAM_PATH = "E:/serverfile/upload/student";
	@Autowired
	private UserService service;
	
	@RequestMapping("/student.html")
	public String goToStudent() {
		return "Student";
	}
	
	@RequestMapping("/stuInfo.html")
	public String goToStuInfo() {
		return "StuInfo";
	}
	
	@ResponseBody
	@RequestMapping("/t/uploadStudent")
	public String batchUploadStudent(MultipartFile file, HttpServletRequest request) {
		logger.info("======> 批量添加学生用户");
		JSONObject result = new JSONObject();
		String fileName = "student" + CUtils.getDateTime() + "_" + file.getOriginalFilename();
		File dir = new File(PROGRAM_PATH);
		//判断上传文件的保存目录是否存在
		if (!dir.exists() && !dir.isDirectory()) {
			System.out.println("创建目录");
			dir.mkdirs();
		}
		try {
			File saveFile = new File(PROGRAM_PATH, fileName);
			file.transferTo(saveFile);
			service.addStudent(UserParse.xlsParse(PROGRAM_PATH+"/"+fileName, 0));
			
			result.put("status", Constant.INSTANSE.OK);
			result.put("msg", "批量添加学生用户成功！");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/t/addStudent")
	public String addStudent(B2SUserBean bean) {
		logger.info("======> 添加学生用户");
		JSONObject result = new JSONObject();
		try {
			UserModel model = new UserModel(bean);
			service.addStudent(model);
			if (model.getIsSuccess() != null && model.getIsSuccess()) {
				result.put("status", Constant.INSTANSE.OK);
				result.put("msg", "添加学生用户成功！");
			} else {
				result.put("status", Constant.INSTANSE.FAIL);
				result.put("msg", "添加学生用户失败！");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/t/deleteStudent")
	public String deleteStudent(B2SUserBean bean) {
		logger.info("======> 删除学生用户");
		JSONObject result = new JSONObject();
		try {
			service.deleteStudent(bean.getId());
			result.put("status", Constant.INSTANSE.OK);
			result.put("msg", "删除学生用户成功！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/t/modifyStudent")
	public String modifyStudent(B2SUserBean bean) {
		logger.info("======> 修改学生信息");
		JSONObject result = new JSONObject();
		try {
			UserModel model = new UserModel(bean);
			service.modifyStudent(model);
			if (model.getIsSuccess() != null && model.getIsSuccess()) {
				result.put("status", Constant.INSTANSE.OK);
				result.put("msg", "修改学生信息成功！");
			} else {
				result.put("status", Constant.INSTANSE.FAIL);
				result.put("msg", "修改学生信息失败！");
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
}
